//
//  TrainViewController.m
//  Four_Fit_New
//
//  Created by xalo on 16/7/14.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "TrainViewController.h"
#import "TableHeaderView.h"
#import "MyNView.h"

#import "TrainModel.h"

@interface TrainViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    CGRect headerViewFrame;
}
@property(nonatomic,strong)UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIProgressView *progress;
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (nonatomic,strong) TableHeaderView *headerView;

@property(nonatomic,strong)NSMutableDictionary *infoDic;
@property(nonatomic,strong)UIImageView *barImageView;

//模糊视图
@property(nonatomic,strong)UIVisualEffectView *visuaView;
@property(nonatomic,strong)UILabel *titleLabel;

@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation TrainViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    //导航栏右侧按钮
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithTitle:@"其他" style:UIBarButtonItemStylePlain target:self action:@selector(startAction:)],[[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(startAction:)]];
    //设置title
    self.titleLabel.text = self.myTitle;
    self.navigationItem.titleView = self.titleLabel;
    self.navigationItem.titleView.hidden = YES;
    
    //加载数据
    [self loadData];
    
    //headerView
    self.tableView.tableHeaderView = self.headerView;
    //tableView
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.progress.alpha = 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    //修改导航栏颜色
#warning 改变成透明色.....................................
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
   
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.barImageView removeFromSuperview];
}

//请求数据
- (void)loadData{
    NSString *url = [NSString stringWithFormat:@"http://bea.sportq.com/SFitWeb/sfit/getPlanDet?form.device=&form.flg=0&form.mySex=0&form.phoneType=1&form.planId=%@&form.sex=0&form.sign=3EE0E21907F5AA4FC79E1EB8D75FE406&form.userId=93118&form.version=1.1.5",self.planId];
    
#warning 数据写了缓存 ...  记得关闭
    [[QZRequestManager sharedManager] fetchDataWithUrl:url parameters:nil method:@"GET" needToCaches:YES fetchData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        self.infoDic = dic[@"entRet"][@"entSinP"];
        for (NSDictionary *dic1 in self.infoDic[@"lstStag"]) {
            TrainModel *model = [[TrainModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataSource addObject:model];
        }
    } updateUI:^{
        //bar image
        self.barImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, kWidth, 64)];
        [self.barImageView sd_setImageWithURL:[NSURL URLWithString:self.infoDic[@"imageURL"]]];
        self.barImageView.clipsToBounds = YES;
        self.barImageView.contentMode = UIViewContentModeBottom;
        self.barImageView.alpha = 0;
        [self.navigationController.navigationBar insertSubview:self.barImageView atIndex:0];
        //添加模糊视图
        UIVisualEffectView *visView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
        visView.frame = self.barImageView.bounds;
        [self.barImageView addSubview:visView];
        
        //设置头视图属性
        [self.headerView.imageURL sd_setImageWithURL:[NSURL URLWithString:self.infoDic[@"imageURL"]]];
        self.headerView.individualName.text = self.infoDic[@"individualName"];
        self.headerView.individualTrainDuration.text = [NSString stringWithFormat:@"%@分钟",self.infoDic[@"individualTrainDuration"] ? self.infoDic[@"individualTrainDuration"] : @"0"];
        self.headerView.individualCalorie.text = [NSString stringWithFormat:@"%@千卡",self.infoDic[@"individualCalorie"] ? self.infoDic[@"individualCalorie"] : @"0"];
        NSString *level = nil;
        if ([self.infoDic[@"individualDifficultyLevel"] isEqualToString:@"0"]) {
            level = @"初级";
        }else if ([self.infoDic[@"individualDifficultyLevel"] isEqualToString:@"1"]){
            level = @"中级";
        }else{
            level = @"高级";
        }
        self.headerView.individualDifficultyLevel.text = level;
        [self.tableView reloadData];
    }];
}
- (UIImageView *)backgroungImageUrl:(NSString *)url frame:(CGRect)frame alpha:(CGFloat)alpha{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    [imageView sd_setImageWithURL:[NSURL URLWithString:url]];
    UIVisualEffectView *visView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visView.alpha = alpha;
    visView.frame = imageView.bounds;
    [imageView addSubview:visView];
    return imageView;
}

//开始训练按钮
- (IBAction)startAction:(UIButton *)sender {
    self.progress.alpha = 1.0;
}
#pragma mark  属性懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}
- (TableHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[NSBundle mainBundle] loadNibNamed:@"TableHeaderView" owner:nil options:nil].lastObject;
        _headerView.frame = CGRectMake(0, 0, kWidth, kWidth);
        _headerView.imageURL.clipsToBounds = YES;
        _headerView.imageURL.contentMode = UIViewContentModeScaleAspectFill;
        headerViewFrame = _headerView.frame;
        [_headerView addSubview:self.visuaView];
    }
    return _headerView;
}
- (NSMutableDictionary *)infoDic{
    if (!_infoDic) {
        _infoDic = [NSMutableDictionary dictionary];
    }
    return _infoDic;
}
- (UIVisualEffectView *)visuaView{
    if (!_visuaView) {
        _visuaView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
        _visuaView.frame = CGRectMake(0, 0, kWidth, kWidth);
        _visuaView.alpha = 0;
    }
    return _visuaView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 24)];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
        
    }
    return _titleLabel;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
#pragma tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataSource[section] lstArray].count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = @"";
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return [NSString stringWithFormat:@"%@(%@个动作)",[self.dataSource[section] stagName],[self.dataSource[section] actNum]];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 40)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, kWidth / 2, 30)];
    label.text = [NSString stringWithFormat:@"%@(%@个动作)",[self.dataSource[section] stagName],[self.dataSource[section] actNum]];
    label.textColor = [UIColor colorWithRed:0.68 green:0.68 blue:0.68 alpha:1.00];
    label.font = [UIFont systemFontOfSize:15];
    [view addSubview:label];
    
    return view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y < 0) {
        self.headerView.imageURL.frame = CGRectMake(headerViewFrame.origin.x,scrollView.contentOffset.y, headerViewFrame.size.width, headerViewFrame.size.height - scrollView.contentOffset.y);
    }else if(scrollView.contentOffset.y < self.headerView.height - 64){
        self.navigationItem.titleView.hidden = NO;
        self.barImageView.alpha = 0;
        self.visuaView.alpha = scrollView.contentOffset.y / 256.0;
        self.titleLabel.alpha = scrollView.contentOffset.y / 256.0;
    }
    if (scrollView.contentOffset.y >= self.headerView.height - 64) {
        self.barImageView.alpha = 1;
    }
}

@end
